#include "./leetcode.h"
/*
https://leetcode.cn/problems/unique-number-of-occurrences/
*/
struct HashSet
{
    int key;
    UT_hash_handle hh;
};

struct HashMap
{
    int key;
    int val;
    UT_hash_handle hh;
};


bool uniqueOccurrences(int* arr, int arrSize)
{
    struct HashMap* occur = NULL;
    for (int i = 0; i < arrSize; i++)
    {
        struct HashMap* temp = NULL;
        HASH_FIND_INT(occur, &arr[i], temp);
        if (!temp)
        {
            temp = malloc(sizeof(struct HashMap));
            temp->key = arr[i];
            temp->val = 1;
            HASH_ADD_INT(occur, key, temp);
        }
        else
        {
            (temp->val)++;
        }
    }

    struct HashSet* times = NULL;
    struct HashMap* cur, * cur_temp;
    HASH_ITER(hh, occur, cur, cur_temp)
    {
        struct HashSet* temp = NULL;
        HASH_FIND_INT(times, &(cur->val), temp);
        if (!temp)
        {
            temp = malloc(sizeof(struct HashSet));
            temp->key = cur->val;
            HASH_ADD_INT(times, key, temp);
        }
    }

    return HASH_COUNT(times) == HASH_COUNT(occur);

}